package com.jmlee.controller;


import com.jmlee.component.HostHolder;
import com.jmlee.enums.RoleEnums;
import com.jmlee.pojo.*;
import com.jmlee.service.AItemSecKillService;
import com.jmlee.service.ShopService;
import com.jmlee.util.SystemDateUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class AItemSecKillController {
    @Autowired
    HostHolder hostHolder;
    @Autowired
    AItemSecKillService aItemSecKillService;
    @Autowired
    ShopService shopService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @GetMapping("secKillItemManager")
    public String secKillItemManager(){
        return "secKillItemManager";
    }

    /**
     * 新增秒杀商品
     * @param aItemSecKill
     * @return
     */
    @PostMapping("addSecKillItem")
    @ResponseBody
    public String addSecKillItem(@RequestBody AItemSecKill aItemSecKill){
        Admin admin = hostHolder.getAdmin();
        aItemSecKill.setCreatedTime(SystemDateUtil.getDaDate());
        aItemSecKill.setShopId(Long.valueOf(shopService.queryShopById(admin.getId()).getId()));
        return aItemSecKillService.addSecKillItem(aItemSecKill).toString();
    }




    /**
     * 获取秒杀商品列表
     * @param curPage
     * @param pageSize
     * @param aItemSecKill
     * @return
     */
    @PostMapping("getSecKillItemList")
    @ResponseBody
    public CommonResult getSecKillItemList(String curPage, String pageSize, AItemSecKill aItemSecKill){
        Admin admin = hostHolder.getAdmin();
        Long roleId = admin.getRoleId();
        if (aItemSecKill == null) {
            aItemSecKill = new AItemSecKill();
            if (roleId == RoleEnums.SHOP.getrId()) {
                aItemSecKill.setShopId(Long.valueOf(shopService.queryShopById(admin.getId()).getId()));
            }
        }
        CommonPage commonPage = new CommonPage();
        commonPage.setCount(aItemSecKillService.getRecorderCount(aItemSecKill));
        commonPage.setData(aItemSecKillService.getSecKillItemList(new RowBounds((Integer.parseInt(curPage) - 1) * Integer.parseInt(pageSize), Integer.parseInt(pageSize)),aItemSecKill));
        return CommonResult.success(commonPage,"success");
    }

    /**
     * 改变商品上下架状态
     * @param aItemSecKill
     * @return
     */
    @PostMapping("changeOnOffStatus")
    @ResponseBody
    public String changeOnOffStatus(@RequestBody AItemSecKill aItemSecKill){
        if (aItemSecKill.getOnOffStatus() == 1){
            aItemSecKill.setOnOffStatus(0);
        }else {
            aItemSecKill.setOnOffStatus(1);
        }
        return aItemSecKillService.editSecKillItem(aItemSecKill).toString();
    }

    /**
     * 修改秒杀截止日期
     * @param aItemSecKill
     * @return
     */
    @PostMapping("changeExpiredTime")
    @ResponseBody
    public String changeExpiredTime(@RequestBody AItemSecKill aItemSecKill){
        return aItemSecKillService.editSecKillItem(aItemSecKill).toString();
    }


}
